//
//  TopicTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/2/22.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "TopicTableViewCell.h"

@interface TopicTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, strong)UIButton * timeButton;
@property (nonatomic, strong)UIButton * personButton;

@end

@implementation TopicTableViewCell


- (void)setModel:(TopicModel *)model{
    
    _model = model;
    
    NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.topic_img];
    NSURL * iconUrl = [NSURL URLWithString:iconStr];
    [self.iconImageView sd_setImageWithURL:iconUrl];
    
    self.titleLabel.text = [NSString stringWithFormat:@"#%@#",model.topic_title];
    NSString * time = [NSString stringWithFormat:@" %@",model.topic_inputtime];
    [self.timeButton setTitle:time forState:UIControlStateNormal];
    NSString * count = [NSString stringWithFormat:@" %@",model.comment_count];
    [self.personButton setTitle:count forState:UIControlStateNormal];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor  =ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.backgroundColor = [UIColor grayColor];
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"#世界那么大，我想去看看#";
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.backView addSubview:self.titleLabel];
    
    
    self.timeButton = [self setupButtonWithImg:@"话题时间" WithTitle:@" 08:30"];
    [self.backView addSubview:self.timeButton];
    self.personButton = [self setupButtonWithImg:@"人数" WithTitle:@" 45"];
    [self.backView addSubview:self.personButton];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(70 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.width.height.mas_equalTo(50 * SCREENH6);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.iconImageView);
    }];
    
    [self.timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.bottom.mas_equalTo(self.iconImageView);
    }];
    
    [self.personButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.bottom.mas_equalTo(self.iconImageView);
    }];
}


- (UIButton *)setupButtonWithImg:(NSString *)img WithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0x909090) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    return button;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
